<template>
  <div class="add-comp-body common-content">
    <el-scrollbar style="height: 100%">
      <div class="scrollbar-box">
        <div class="top-search-bar">
          <el-form size="small" ref="queryForm" inline>
            <el-form-item>
              <el-button
                  type="primary"
                  size="small"
                  icon="el-icon-back"
                  @click="$router.go(-1)">返回
              </el-button>
              <el-button
                  type="primary"
                  size="small"
                  @click="$router.replace({name:'FunList',query:{id:$route.query.id}})">函数列表
              </el-button>
            </el-form-item>
          </el-form>
        </div>

        <el-form
            :model="form"
            :rules="basicRules"
            ref="basicForm"
            label-width="100px"
            class="demo-ruleForm">

          <div class="comp-item-group">
            <div class="comp-item-box">
              <div class="comp-item-title">组件基本信息</div>
              <div class="comp-item-cont">
                <el-row :gutter="15">
                  <el-col :span="8">
                    <el-form-item label="组件名称" prop="pluginsName">
                      <el-input v-model="form.pluginsName" placeholder="请填写组件名称"></el-input>
                    </el-form-item>
                  </el-col>
                  <el-col :span="8">
                    <el-form-item label="组件版本号" prop="pluginsVesion">
                      <el-input v-model="form.pluginsVesion" placeholder="请填写组件版本号"></el-input>
                    </el-form-item>
                  </el-col>

                  <el-col :span="8">
                    <el-form-item label="组件类型">
                      <el-select
                          class="w100"
                          ref="mysel"
                          v-model="form.libraryId"
                          placeholder="选择组件类型">
                        <el-option
                            v-for="item in typeOptions"
                            :key="item.id"
                            :label="item.libraryName"
                            :value="item.id">
                          <span>{{ item.libraryName }}</span>
                        </el-option>
                        <div class="text-center ptb5" @click="libraryAdd">
                          <el-button type="primary" size="mini">新增分类</el-button>
                        </div>
                      </el-select>
                    </el-form-item>
                  </el-col>

                  <el-col :span="8">
                    <el-form-item label="数据入库" prop="isIndb">
                      <el-select
                          class="w100"
                          v-model="form.isIndb"
                          placeholder="数据是否入库">
                        <el-option label="入库" :value="0"></el-option>
                        <el-option label="不入库" :value="1"></el-option>
                      </el-select>
                    </el-form-item>
                  </el-col>

                  <el-col :span="8">
                    <el-form-item label="是否监听" prop="isMonitor">
                      <el-select
                          class="w100"
                          v-model="form.isMonitor"
                          placeholder="数据是否监听">
                        <el-option label="监听" :value="0"></el-option>
                        <el-option label="不监听" :value="1"></el-option>
                      </el-select>
                    </el-form-item>
                  </el-col>

                  <el-col :span="8">
                    <el-form-item label="是否切换" prop="isSwitch">
                      <el-select
                          class="w100"
                          v-model="form.isSwitch"
                          placeholder="数据是否切换">
                        <el-option label="切换" :value="0"></el-option>
                        <el-option label="不切换" :value="1"></el-option>
                      </el-select>
                    </el-form-item>
                  </el-col>

                  <el-col :span="8">
                    <el-form-item label="版本说明" prop="pluginsRemarks">
                      <el-input
                          type="input"
                          placeholder="请填写组件版本说明"
                          v-model="form.pluginsRemarks"></el-input>
                    </el-form-item>
                  </el-col>

                  <el-col :span="8">
                    <el-form-item label="场景描述" prop="pluginsScene">
                      <el-input
                          type="input"
                          placeholder="请填写组件场景描述"
                          v-model="form.pluginsScene"
                      ></el-input>
                    </el-form-item>
                  </el-col>

                  <el-col :span="8">
                    <el-form-item label="备注" prop="remarks">
                      <el-input type="input" placeholder="请填写组件备注" v-model="form.remarks"></el-input>
                    </el-form-item>
                  </el-col>

                  <el-col :span="8">
                    <el-form-item class="up" label="上传组件">
                      <el-input v-model="form.pluginsPath" placeholder="请选择上传组件">
                        <el-upload
                            slot="append"
                            class="upload-demo"
                            action="#"
                            :show-file-list="false"
                            :http-request="getFile">
                          <el-button size="small" type="primary">点击上传</el-button>
                        </el-upload>
                      </el-input>
                    </el-form-item>
                  </el-col>
                </el-row>
              </div>
            </div>

            <div class="comp-item-box">
              <div class="comp-item-title">组件函数</div>
              <div class="comp-item-cont">
                <el-form-item label="" prop="pluginsName">
                  <div style="line-height: 1;">
                    <RegisterFunList ref="registerFunList" :datas="funList"></RegisterFunList>
                  </div>
                </el-form-item>
              </div>
            </div>

            <div class="comp-item-box">
              <div class="comp-item-title">运行环境</div>
              <div class="comp-item-cont">
                <el-row :gutter="15">
                  <el-col :span="8">
                    <el-form-item label="使用系统" prop="systemId">
                      <el-select
                          class="w100"
                          v-model="form.systemId"
                          placeholder="请选择使用系统">
                        <el-option
                            v-for="item in systemOptions"
                            :key="item.id"
                            :label="item.systemName"
                            :value="item.id">
                        </el-option>
                      </el-select>
                    </el-form-item>
                  </el-col>
                  <el-col :span="8">
                    <el-form-item label="最小CPU">
                      <el-input
                          type="number"
                          placeholder="请填写最小CPU"
                          v-model="form.requireCPU"></el-input>
                    </el-form-item>
                  </el-col>
                  <el-col :span="8">
                    <el-form-item label="最小存储单元">
                      <el-input
                          type="number"
                          placeholder="请填写最小存储单元"
                          v-model="form.requireDisk"></el-input>
                    </el-form-item>
                  </el-col>

                  <el-col :span="8">
                    <el-form-item label="最小GPU">
                      <el-input
                          type="number"
                          placeholder="请填写最小GPU"
                          v-model="form.requireGPU"></el-input>
                    </el-form-item>
                  </el-col>

                  <el-col :span="8">
                    <el-form-item label="运行内存">
                      <el-input
                          type="number"
                          placeholder="请填写运行内存"
                          v-model="form.requireMemory"></el-input>
                    </el-form-item>
                  </el-col>

                  <el-col :span="8">
                    <el-form-item label="备注">
                      <el-input
                          type="input"
                          placeholder="请填写运行备注"
                          v-model="form.requireRemarks"></el-input>
                    </el-form-item>
                  </el-col>

                  <el-col :span="8">
                    <el-form-item class="up" label="上传环境">
                      <el-input v-model="form.requirePath" placeholder="请上传运行环境">
                        <el-upload
                            slot="append"
                            class="upload-demo"
                            action="#"

                            :show-file-list="false"
                            :http-request="environmentFile">
                          <el-button size="small" type="primary">点击上传</el-button>
                        </el-upload>
                      </el-input>
                    </el-form-item>
                  </el-col>
                </el-row>
              </div>
            </div>

            <div class="comp-item-box" v-if="form.isMonitor === 0">
              <div class="comp-item-title">监控信息</div>
              <div class="comp-item-cont">
                <el-row :gutter="15">
                  <el-col :span="8">
                    <el-form-item label="监控频率/s">
                      <el-input v-model="form.monitorRate" placeholder="请填写监控频率,如：10"></el-input>
                    </el-form-item>
                  </el-col>
                  <el-col :span="8">
                    <el-form-item label="监控类型">
                      <el-select
                          class="w100"
                          v-model="form.monitorType"
                          placeholder="请选择监控类型">
                        <el-option label="全部" :value="2"></el-option>
                        <el-option label="进程监控" :value="0"></el-option>
                        <el-option label="数据监控" :value="1"></el-option>
                      </el-select>
                    </el-form-item>
                  </el-col>
                  <el-col :span="24" v-if="form.monitorType!==''">
                    <el-form-item label="监控类型">
                      <div style="line-height: 1;">
                        <MonitorList ref="monitorList" :paroles="form.paroles" :type="form.monitorType"></MonitorList>
                      </div>
                    </el-form-item>
                  </el-col>
                </el-row>
              </div>
            </div>

            <div class="comp-item-box" v-if="form.isSwitch===0">
              <div class="comp-item-title">故障切换信息</div>
              <div class="comp-item-cont">
                <el-row :gutter="15">
                  <el-col :span="8">
                    <el-form-item label="组件切换名称">
                      <el-input v-model="form.switchName" placeholder="请填写组件切换名称"></el-input>
                    </el-form-item>
                  </el-col>
                  <el-col :span="8">
                    <el-form-item label="组件切换地址">
                      <el-input v-model="form.switchAddress" placeholder="请填写组件切换地址"></el-input>
                    </el-form-item>
                  </el-col>
                </el-row>
              </div>
            </div>

            <div class="comp-item-btn-group">
              <!--<el-button type="warning" @click="resetbasicForm()">重置数据</el-button>-->
              <el-button type="primary" @click="submitbasicForm()" :loading="btnLoading">保存修改</el-button>
            </div>
          </div>
        </el-form>
      </div>
    </el-scrollbar>

    <SuccessDailog ref="successDailog" @test="testTap"></SuccessDailog>
    <TestDailog ref="testDailog" @testResult="testResult"></TestDailog>
    <TestResultDailog ref="testResultDailog"></TestResultDailog>
    <AddTypeDailog ref="addTypeDailog" @success="getType"></AddTypeDailog>
  </div>
</template>

<script>
import {
  get_getSystem,
  put_libraryAdd,
  get_getPLibrarySelect,
  put_updatePluginsInfo
} from "@/apis/assembly/assembly";

import {get_getPlugins} from "@/apis/comList/comList";

import RegisterFunList from '../register/comp/FunList'
import MonitorList from '../register/comp/MonitorList'
import SuccessDailog from '../register/comp/SuccessDailog'
import TestDailog from '../register/comp/TestDailog'
import TestResultDailog from '../register/comp/TestResultDailog'
import AddTypeDailog from '../register/comp/AddTypeDailog'

export default {
  components: {RegisterFunList, MonitorList, SuccessDailog, TestDailog, TestResultDailog, AddTypeDailog},
  directives: {},
  data() {
    return {
      form: {
        //基本信息表单
        pluginsName: "",
        pluginsVesion: "",
        libraryId: '',
        isIndb: "",
        isMonitor: "",
        isSwitch: "",
        pluginsRemarks: "",
        pluginsScene: "",
        remarks: "",
        pluginsPath: "",

        // 运行环境
        requireCPU: '',
        requireDisk: '',
        requireGPU: '',
        requireMemory: '',
        requireRemarks: '',
        requirePath: '',

        // 监控信息
        monitorRate: '',
        monitorType: '',

        // 故障切换信息
        switchName: '',
        switchAddress: '',
      },

      funList: [],

      //基本信息验证
      basicRules: {},
      logData: [], // 录入数据返回结果

      result: [], // 测试结果
      resultVisble: false,
      systemOptions: [],
      typeOptions: [],
      btnLoading: false
    }
  },
  mounted() {
    this.getInfo();
    this.getSystem();
    this.getType();
  },
  computed: {},
  methods: {
    getInfo() {
      get_getPlugins({pluginsId: this.$route.query.id}).then((res) => {
          let {code, data} = res.data;
          if (code === 0) {
            this.listInfo = data;
            let {plugins, parequire, pamonitor, paswitch, pfunctionList} = data
            //基本信息表单
            this.form.id = plugins && plugins.id
            this.form.pluginsName = plugins && plugins.pluginsName
            this.form.pluginsVesion = plugins && plugins.pluginsVesion
            this.form.libraryId = plugins && plugins.pl.libraryId
            this.form.isIndb = plugins && plugins.isIndb
            this.form.isMonitor = plugins && plugins.isMonitor
            this.form.isSwitch = plugins && plugins.isSwitch
            this.form.pluginsRemarks = plugins && plugins.pluginsRemarks
            this.form.pluginsScene = plugins && plugins.pluginsScene
            this.form.pluginsPath = plugins && plugins.pluginsPath
            this.form.remarks = plugins && plugins.remarks

            // 运行环境
            this.form.parequireId = parequire && parequire.id
            this.form.requireCPU = parequire && parequire.requireCPU
            this.form.requireDisk = parequire && parequire.requireDisk
            this.form.requireGPU = parequire && parequire.requireGPU
            this.form.requireMemory = parequire && parequire.requireMemory
            this.form.requireRemarks = parequire && parequire.requireRemarks
            this.form.requirePath = parequire && parequire.requirePath

            // 监控信息
            this.form.pamonitorId = pamonitor && pamonitor.id
            this.form.monitorRate = pamonitor && pamonitor.monitorRate
            this.form.monitorType = pamonitor && pamonitor.monitorType
            this.form.paroles = pamonitor && pamonitor.paroles || []

            // 故障切换信息
            this.form.paswitchId = paswitch && paswitch.id
            this.form.switchName = paswitch && paswitch.switchName
            this.form.switchAddress = paswitch && paswitch.switchAddress

            this.funList = pfunctionList.map(item => {
              // addTime: 1659605390000
              // editTime: null
              // functionName: "temperature"
              // id: "1555123835496431617"
              // paramInputClass: "Double ve,Double norm"
              // paramOutputClass: "Double"
              // pid: "1555123835425128448"
              // remarks: ""
              // userName: "admin"

              return {
                id: item.id,
                pid: item.pid,
                name: item.functionName,
                putParams: item.paramInputClass,
                outParams: item.paramOutputClass,
                putParamsObj: item.paramInputClass.split(',').map(li => {
                  return {
                    name: li.split(' ')[0] || '',
                    value: li.split(' ')[1] || ''
                  }
                }),
                oputParamsObj: item.paramOutputClass.split(',').map(li => {
                  return {
                    name: li.split(' ')[0] || '',
                    value: li.split(' ')[1] || ''
                  }
                }),
                remarks: item.remarks
              }
            })
          }
        }
      )
    },
    // 系统下拉列表
    getSystem() {
      get_getSystem().then((res) => {
        this.systemOptions = res.data.data;
      });
    },
    // 获取组件类型
    getType() {
      get_getPLibrarySelect().then((res) => {
        this.typeOptions = res.data.data;
      });
    },
    // 新增组件分类
    libraryAdd() {
      this.$refs.addTypeDailog.show()
    },
    // 录入基本信息组件
    getFile(data) {
      let formData = new FormData()
      formData.append('file', data.file)
      this.$api.uploadPluginsFile(formData).then(res => {
        if (res.data.code === 0) {
          this.form.pluginsPath = res.data.data.pluginsPath
          this.form.fileName = res.data.data.fileName
          this.form.pluginsSuffix = res.data.data.pluginsSuffix
        }
      })
    },
    // 录入环境组件
    environmentFile(data) {
      let formData = new FormData()
      formData.append('file', data.file)
      this.$api.uploadRequireFile(formData).then(res => {
        if (res.data.code === 0) {
          this.form.requirePath = res.data.data.requirePath
        }
      })
    },
    // 结果确定
    resultEnt() {
      this.$confirm("是否将此组件纳入库?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(() => {
        put_libraryAdd({pluginsId: [this.logData.pid]}).then((res) => {
          let {code, msg} = res.data;
          if (code == 0) {
            this.$message.success(msg);
            this.resultVisble = false;
            this.getList();
            this.$router.push("/zjgl");
          }
        });
      }).catch(() => {
        this.$message({
          type: "info",
          message: "已取消纳入组件库",
        });
      });
    },
    //录入基本信息
    submitbasicForm() {
      let registerFunDatas = this.$refs.registerFunList && this.$refs.registerFunList.lists
      let monitorDatas = this.$refs.monitorList && this.$refs.monitorList.lists
      let datas = {}

      datas.plugins = {
        id: this.form.id,
        pluginsName: this.form.pluginsName,
        pluginsVesion: this.form.pluginsVesion,
        pl: {libraryId: this.form.libraryId},
        isIndb: this.form.isIndb,
        isMonitor: this.form.isMonitor,
        isSwitch: this.form.isSwitch,
        pluginsRemarks: this.form.pluginsRemarks,
        pluginsScene: this.form.pluginsScene,
        remarks: this.form.remarks,
        pluginsPath: this.form.pluginsPath,
        fileName: this.form.fileName,
        pluginsSuffix: this.form.pluginsSuffix,
      }
      let pfunctionDatas = registerFunDatas.length ? registerFunDatas.map(item => {
        return {
          functionName: item.name,
          paramInputClass: item.putParams,
          paramOutputClass: item.outParams,
          remarks: item.remarks,
          pStructureList: item.pStructureList || [],
          outStructureList: item.outStructureList || []
        }
      }) : []


      let filterPfunctionDatas = pfunctionDatas.filter(item => item.functionName)
      if (filterPfunctionDatas.length) {
        datas.pfunction = filterPfunctionDatas
      }

      datas.require = {
        id: this.form.parequireId,
        systemId: this.form.systemId,
        requireCPU: this.form.requireCPU,
        requireDisk: this.form.requireDisk,
        requireGPU: this.form.requireGPU,
        requireMemory: this.form.requireMemory,
        requireRemarks: this.form.requireRemarks,
        requirePath: this.form.requirePath
      }
      if (this.form.monitorRate && this.form.monitorType) {
        datas.monitor = {
          id: this.form.pamonitorId,
          monitorRate: this.form.monitorRate,
          monitorType: this.form.monitorType,
        }
      }
      if (this.form.switchName && this.form.switchAddress) {
        datas.switch = {
          id: this.form.pamonitorId,
          switchName: this.form.switchName,
          switchAddress: this.form.switchAddress,
        }
      }
      if (this.form.isMonitor === 0) {
        datas.roles = monitorDatas ? monitorDatas.map(item => {
          return {
            roleType: item.roleType,
            roleName: item.roleName,
            roleProcessName: item.roleProcessName
          }
        }) : []
      }

      this.btnLoading = true
      put_updatePluginsInfo(datas).then(res => {
        if (res.data.code === 0) {
          this.$refs.successDailog.show(res.data.data)
        }
      }).finally(() => {
        this.btnLoading = false
      })
    },
    // 检测
    testTap(appid) {
      this.$refs.testDailog.show(appid)
    },
    // 检测结果
    testResult(data) {
      this.$refs.testResultDailog.show(data)
    }
  }
}
</script>

<style lang="less" scoped>
  .add-comp-body {
    height: 100%;
    width: 100%;

    .scrollbar-box {
      padding: 15px;
    }

    .comp-item-group {
      .comp-item-box {
        margin-bottom: 15px;

        .comp-item-title {
          height: 40px;
          color: #ffffff;
          display: flex;
          align-items: center;
          justify-content: center;
          background-color: rgba(3, 68, 108, 0.8);
          font-size: 17px;
          font-weight: bold;
        }

        .comp-item-cont {
          padding: 15px;
          border: solid 1px rgba(3, 68, 108, 0.5);
        }
      }

      .comp-item-btn-group {
        text-align: center;
        margin-top: 15px;
      }

      /deep/ .el-input-group__append {
        background-color: #66b1ff;
        border-color: #66b1ff;
        color: #ffffff;
      }
    }
  }

  .libBody {
    display: flex;
    justify-content: center;
    align-items: center;

    span {
      white-space: nowrap;
      margin-right: 15px;
    }
  }

  .fncIte {
    .item {
      margin-bottom: 20px;
      display: flex;

      span {
        display: inline-block;
        width: 50%;
      }
    }
  }


  .jdt {
    width: 50%;
    margin: 0 auto;
  }

  .fncItem {
    margin-bottom: 10px;
    text-align: center;
    width: 100%;

    .lab {
      width: 100px;
      display: block;
    }

    .fncType {
      margin-top: 10px;
      display: flex;

      div {
        width: 80%;
        height: 80px;
        border: 1px solid rgb(83, 82, 82);
        display: flex;
        flex-wrap: wrap;
        overflow: scroll;

        .type {
          display: block;
          width: 100px;
          line-height: 30px;
          text-align: center;
          margin-right: 10px;
          margin-bottom: 5px;
        }
      }
    }

    .fncTest {
      margin-top: 10px;
      display: flex;

      /deep/ .el-textarea {
        width: 80% !important;

        /deep/ .el-textarea__inner {
          width: 100% !important;
        }
      }
    }
  }


  .paramForm {
    /deep/ .el-form-item__label {
      white-space: nowrap;
    }

    /deep/ .el-form-item__content {
      margin-left: 0 !important;
    }

    .dia {
      width: 100%;
      display: flex;
      justify-content: center;
    }
  }

  .testBody {
    max-height: 500px;
    overflow: scroll;
  }

  .zjmingc {
    padding: 0 15px;
  }

  .dfi {
    display: flex;
    margin-top: 15px;

    span {
      margin-right: 20px;
    }
  }

  .mrnum {
    margin-right: 150px;
  }

  .inpwid {
    width: 1340px;
  }

  .df {
    display: flex;
    margin-top: 10px;
    margin-left: 25px;
  }

  .cont {
    display: flex;
    flex-direction: column;
    height: 100%;
    justify-content: space-between;

    .cont-bottom {
      /*background: white;*/
      height: 40px;
      line-height: 40px;
      padding: 0 15px;
    }

    /deep/ .el-collapse-item__header {
      position: relative;
    }

    .cont-main {
      margin: 15px 15px 0 15px;
      /*background: white;*/
      background-color: rgba(0, 0, 0, 0.1);
      height: calc(100vh - 120px);
      padding: 15px;
      overflow: scroll;

      /deep/ .el-collapse-item__arrow {
        margin: 0 8px 0 8px;
      }

      .collapseTitle {
        margin-left: 10px;
      }

      .interfaceItems {
        margin-top: 30px;
      }

      .cont_body {
        margin-top: 15px;

        .timeLine {
          width: 1000px;
          margin: 0 auto;
          padding: 15px 0;

          /deep/ .el-step__title {
            margin-left: -10px;
          }
        }
      }
    }

    .basic {
      display: flex;
      align-items: center;

      .dfc {
        margin-right: 30px;
      }
    }

    .files {
      padding: 0 15px;
      display: flex;
    }

    .addbtn {
      margin-left: 10px;
    }

    .waydiv {
      padding: 0 15px;
    }

    .waylist {
      margin-right: 20px;
    }

    .systm {
      padding: 0 15px;
    }

    .min {
      margin-right: 30px;
    }

    .btn-class {
      background: #267ff6;
      border: none;
      color: white;
      border-radius: 15px;
      line-height: 30px;
      height: 30px;
      min-width: 100px;
      position: absolute;
      left: 50%;
      /*margin-left: -50px;*/
      transform: translateX(-50%);
      cursor: pointer;
    }

    .wid {
      width: 500px;
    }

    .viewbox {
      width: 100%;
      border: 1px solid #dcdcdc;
      position: relative;
      padding: 30px 10px 10px 10px;
      // margin-bottom: 15px;
      .sptitle {
        position: absolute;
        top: -12px;
        background: white;
        left: 10px;
      }

      .jkming {
        margin-right: 50px;
        padding-left: 12px;
        margin-bottom: 15px;
      }
    }
  }

  .library {
    float: right;
    margin: 10px;
  }
</style>
